using System.Collections.Generic;
using UnityEngine;
using EffectTypeValue = System.Int32;
using BgmTypeValue = System.Int32;

namespace Life.ESound
{
    /// <summary>
    /// 声音片段库
    /// </summary>
    [CreateAssetMenu(fileName = "SoundClips", menuName = "WorkFlow/SoundClips")]
    public class SoundClips : ScriptableObject
    {
        [System.Serializable]
        private class EffectClip
        {
            [EffectSelect]
            public EffectTypeValue type;
            public AudioClip clip;
        }
        [System.Serializable]
        private class BgmClip
        {
            [BgmSelect]
            public BgmTypeValue type;
            public AudioClip clip;
        }
        
        [SerializeField]
        private List<BgmClip> bgmClips;
        [SerializeField]
        private List<EffectClip> effectClips;

        private readonly Dictionary<EffectTypeValue, AudioClip> _effectClipDict = new Dictionary<EffectTypeValue, AudioClip>();

        private readonly Dictionary<BgmTypeValue, AudioClip> _bgmClipDict = new Dictionary<BgmTypeValue, AudioClip>();

        public void Initialize()
        {
            if (_effectClipDict.Count == 0)
            {
                _effectClipDict.Clear();
                foreach (var clip in effectClips)
                {
                    _effectClipDict.Add(clip.type, clip.clip);
                }
            }
            if (_bgmClipDict.Count == 0)
            {
                _bgmClipDict.Clear();
                foreach (var clip in bgmClips)
                {
                    _bgmClipDict.Add(clip.type, clip.clip);
                }
            }

#if !UNITY_EDITOR
            effectClips.Clear();
#endif
        }
        /// <summary>
        /// 根据类型获取声音片段 如果不存在将返回空
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public AudioClip GetEffectClip(EffectTypeValue type)
        {
            if (_effectClipDict.TryGetValue(type, out var clip))
            {
                return clip;
            }
#if UNITY_EDITOR
            //Editor下，如果没有找到声音片段，将会报错，用于提示开发者
            Debug.LogError("Can't find clip by type: " + type);
#endif
            return null;
        }
        public AudioClip GetBgmClip(BgmTypeValue type)
        {
            if (_bgmClipDict.TryGetValue(type, out var clip))
            {
                return clip;
            }
#if UNITY_EDITOR
            //Editor下，如果没有找到声音片段，将会报错，用于提示开发者
            Debug.LogError("Can't find clip by type: " + type);
#endif
            return null;
        }
    }
}
